package cn.wgx.modules.sys.services;

import cn.wgx.modules.base.services.BaseService;
import cn.wgx.modules.sys.entity.SysUserPermission;
import cn.wgx.modules.sys.entity.SysUserPermissionExample;
import cn.wgx.modules.sys.entity.mapper.SysUserPermissionMapper;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class SysUserPermissionService extends BaseService<SysUserPermission> {

    @Resource
    SysUserPermissionMapper sysUserPermissionMapper;

    @Transactional(readOnly = true)
    public List<SysUserPermission> getList(SysUserPermission record, Boolean desc) {
		SysUserPermissionExample example = new SysUserPermissionExample();
		if(record != null){
            SysUserPermissionExample.Criteria criteria = example.createCriteria();
            if(record.getUserId() != null){
                criteria.andUserIdEqualTo(record.getUserId());
            }
        }
		example.setOrderByClause("createDate " + (desc?"desc": "asc"));
		return sysUserPermissionMapper.selectByExample(example);
    }

    @Transactional(readOnly = true)
    public List<SysUserPermission> getListByIds(List<Integer> ids) {
        if(ids == null || ids.isEmpty()){
            return emptyList;
        }
        List<Integer> uniList = getUniList(ids);
        if(!uniList.isEmpty()){
            SysUserPermissionExample example = new SysUserPermissionExample();
            SysUserPermissionExample.Criteria criteria = example.createCriteria();
            criteria.andUser_permission_idIn(uniList);
            List<SysUserPermission> eduCourseAssignmentHis = sysUserPermissionMapper.selectByExample(example);
            return eduCourseAssignmentHis;
        }
        return null;
    }

    @Transactional(readOnly = true)
    public SysUserPermission getOne(Integer id) {
		return sysUserPermissionMapper.selectByPrimaryKey(id);
    }

    @Transactional
    public SysUserPermission insertOne( SysUserPermission record) {
        sysUserPermissionMapper.insertSelective(record);
        return record;
    }

    @Transactional
    @CacheEvict(value = cacheName, allEntries = true)
    public Integer updateUserPermission(List<SysUserPermission> sysDutyPermissions) {
        if(sysDutyPermissions.size() > 0){
            SysUserPermissionExample example = new SysUserPermissionExample();
            example.createCriteria().andUserIdEqualTo(sysDutyPermissions.get(0).getUserId());
            sysUserPermissionMapper.deleteByExample(example);
            int n = 0;
            for(SysUserPermission sysDutyPermission: sysDutyPermissions){
                filter(sysDutyPermission, true);//sysUserPermissionMapper.Column.createDate
                sysUserPermissionMapper.insertSelective(sysDutyPermission);
                n ++;
            }
//            sysDutyPermissions.get(0).SysDutyPermission.Column.excludes(SysDutyPermission.Column.duty_per_id)
//            mapper.batchInsertSelective(sysDutyPermissions,SysDutyPermission.Column.duty_id, SysDutyPermission.Column.permission_id );
            return n;
        }
        return 0;
    }
    @Transactional
    public SysUserPermission updateOne( SysUserPermission record) {
        sysUserPermissionMapper.updateByPrimaryKeySelective(record);
        return record;

    }


    @Transactional
    public int deleteOne( Integer id) {
        return sysUserPermissionMapper.deleteByPrimaryKey(id);
    }

	

}